<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%@ page import="net.sf.jsr107cache.*"%>
<%@ page import="org.feeddreamwork.fetcher.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ include file="/includes/head.jsp"%>
<title>FeedDreamwork - 管理员工具</title>
</head>
<body>
<%@ include file="/includes/top.jsp"%>
<div id="menu">
<ul>
	<li><a href="/">主页</a></li>
	<li><a href="/settings/listfeed.jsp">我的 Feed</a></li>
	<li><a href="/settings/addfeed.jsp">添加 Feed</a></li>
	<li class="active"><a href="/admin/admin.jsp">管理员工具</a></li>
	<%@ include file="/includes/userinfo.jsp"%>
</ul>
</div>
<div id="page">
<h2>Feed 缓存</h2>
<%
	CacheStatistics stats = FeedResultCache.getCacheStatistics();
	int hits = stats.getCacheHits();
	int misses = stats.getCacheMisses();
	double hitRate = hits + misses != 0 ? (double) hits
			/ (hits + misses) * 100 : 0;
%>
<p>当前 Feed 缓存中共有 <%=stats.getObjectCount()%> 项，命中 <%=hits%> 次，未命中 <%=misses%>
次，命中率为 <%=String.format("%.2f", hitRate)%>%</p>
<h2>页面缓存</h2>
<form action="/tasks/clearcache" method="post">
<p><input type="submit" value="清除缓存" /></p>
</form>
<%
	PageInfoRepository pageInfoRepo = new PageInfoRepository();
	List<PageInfo> pageInfos = pageInfoRepo.getPageInfos();
	int finished = 0;
	int total = pageInfos.size();
	for (PageInfo pageInfo : pageInfos)
		if (pageInfo.isFinished())
			finished++;

	int itemPerPage = 50;
	int pageNum = 1;
	String pageNumStr = request.getParameter("page");
	if (!Utils.isNullOrEmpty(pageNumStr))
		pageNum = Integer.parseInt(pageNumStr);
%>
<p>当前共有 <%=total%> 项缓存，其中 <%=total - finished%> 项还未下载，<%=finished%>
项已完成</p>
<table>
	<tr>
		<td>ID</td>
		<td>地址</td>
		<td>添加时间</td>
		<td>状态</td>
		<td>完成时间</td>
	</tr>
	<%
		int current = 0;
		for (PageInfo pageInfo : pageInfos) {
			current++;
			if (current > pageNum * itemPerPage)
				break;
			if (current >= (pageNum - 1) * itemPerPage) {
	%>
	<tr>
		<td><%=current%></td>
		<td><%=pageInfo.getUrl()%></td>
		<td><%=DateUtils.formatDateAsSimpleDateTime(pageInfo
							.getAddTime())%></td>
		<%
			if (pageInfo.isFinished()) {
		%>
		<td>完成</td>
		<td><%=DateUtils.formatDateAsSimpleDateTime(pageInfo
								.getFinishTime())%></td>
		<%
			} else {
		%>
		<td>等待</td>
		<td></td>
		<%
			}
		%>
	</tr>
	<%
		}
		}
	%>
</table>
<p class="pageNumber">
<%
	int pages = (int) Math.ceil((double) total / itemPerPage);
	for (int i = 1; i <= pages; i++) {
		if (i != pageNum) {
%> <a href="/admin/admin.jsp?page=<%=i%>">第 <%=i%> 页</a> <%
 	} else {
 %> 第 <%=i%> 页 <%
 	}
 	}
 %>
</p>
</div>
<%@ include file="/includes/footer.jsp"%>
</body>
</html>
